const { json } = require("express");
const { sqlPromise } = require("../db/sqlPromise");

exports.addfile = async (req, res) => {
  console.log(req.body);
  console.log("--------------------------");
  console.log(req.file);
  if (req.file.length === 0) {  
      //判断一下文件是否存在，也可以在前端代码中进行判断。
    res.send("error", {message: "上传文件不能为空！"}) 
};

  let filename = req.file.filename

  let sql = `UPDATE sp_user SET avatar=? WHERE user_id = ?`;
  let results = await sqlPromise(sql, [`uploads/${filename}`, req.params.id]);

  res.send({
    data: {
      tmp_path: `uploads/${filename}`,
      url: `http://127.0.0.1:3000/uploads/${filename}`,
    },
    meta: {
      msg: "上传成功",
      status: 200,
    },
  });
};
